package Region.socket;

import Region.DatabaseManager;
import Region.LogManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

public class RegionToRegionSendThread implements Runnable{
    private Socket socket;
    public BufferedReader input = null;
    public PrintWriter output = null;
    private DatabaseManager dataBaseManager;
    private LogManager logManager;

    public RegionToRegionSendThread(Socket socket, DatabaseManager dataBaseManager, LogManager logManager) throws IOException {
        this.socket = socket;
        this.logManager = logManager;
        this.dataBaseManager = dataBaseManager;

        this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        this.output = new PrintWriter(socket.getOutputStream());
    }

    @Override
    public void run(){
        try {
            System.out.println("开始发送消息");
            String tableName = input.readLine();
            ArrayList<String> logContent = new ArrayList<>();
            logContent = logManager.readLog(tableName);
            for(String str:logContent){
                output.println(str);
            }
            output.println("END");
            output.flush();
//            output.close();
//            input.close();
//            socket.close();

            System.out.println("log发送完成");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
